﻿using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using 历史记录.Helper;

namespace 历史记录.DataAccessLayer
{
    /// <summary>
    /// 支持C# 所有类型
    /// </summary>
    public class LiteDBClient<T> : DisposableBase, IDAL<T>
           where T : Model, new()
    {
        private LiteDatabase Db { get; }

        internal LiteDBClient(string connectionString)
        {
            Db = new LiteDatabase(connectionString);
        }

        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="table">表名</param>
        /// <returns></returns>
        public bool DropTable(string table)
        {
            return Db.DropCollection(table);
        }

        /// <summary>
        /// 刪除记录
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="predicate">删除条件</param>
        /// <returns></returns>
        public bool DeleteMany(string table, Expression<Func<T, bool>> predicate)
        {
            return Db.GetCollection<T>(table).DeleteMany(predicate) >= 0;
        }

        /// <summary>
        /// 获取表名称列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetTableList()
        {
            return Db.GetCollectionNames().ToList();
        }

        /// <summary>
        /// 创建新的表
        /// </summary>
        /// <param name="table">表名</param>
        public void InitTables(string table)
        {
        }

        /// <summary>
        /// 插入列表
        /// </summary>
        /// <param name="table">插入表名称</param>
        /// <param name="insertObjs">插入数据列表</param>
        /// <returns></returns>
        public bool Insertable(string table, List<T> insertObjs)
        {
            var col = Db.GetCollection<T>(table);
            return col.InsertBulk(insertObjs) > 0;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <typeparam name="TResult">返回的值的类型</typeparam>
        /// <param name="table">表名</param>
        /// <param name="Select">一个应用于每个源元素的转换函数</param>
        /// <param name="Where">查询条件</param>
        /// <returns></returns>
        public IEnumerable<TResult> Queryable<TResult>(string table, Expression<Func<T, TResult>> Select, Expression<Func<T, bool>> Where = null) where TResult : new()
        {
            return Queryable(table, Where).Select(Select.Compile());
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="Where">查询条件</param>
        /// <returns></returns>
        public IEnumerable<T> Queryable(string table, Expression<Func<T, bool>> Where = null)
        {
            var col = Db.GetCollection<T>(table);
            IEnumerable<T> Queryable;
            if (Where == null)
                Queryable = col.FindAll();
            else Queryable = col.Find(Where);
            return Queryable;
        }

        /// <summary>
        /// 最晚时间
        /// </summary>
        /// <param name="table">表名</param>
        /// <returns></returns>
        public DateTime Max(string table)
        {
            return Db.GetCollection<Model>(table).Max(a => a.Id);
        }

        /// <summary>
        /// 最早时间
        /// </summary>
        /// <param name="table">表名</param>
        /// <returns></returns>
        public DateTime Min(string table)
        {
            return Db.GetCollection<Model>(table).Min(a => a.Id);
        }

        protected override void OnDispose()
        {
            Db.Dispose();
        }
    }
}